<template>
  <div>
    <v-header></v-header>
    <div class="dis10"></div>
    <div class="listbox aps8">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="原密码" prop="old_password">
          <el-input type="password" v-model="ruleForm.old_password" autocomplete="off" class="w300"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="new_password">
          <el-input type="password" v-model="ruleForm.new_password" autocomplete="off" class="w300"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="cur_password">
          <el-input type="password" v-model="ruleForm.cur_password" autocomplete="off" class="w300"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.cur_password !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.new_password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        old_password: "",
        new_password: "",
        cur_password: ""
      },
      rules: {
        old_password: [
          { required: true, message: "请输入原密码", trigger: "blur" }
        ],
        new_password: [
          { required: true, validator: validatePass, trigger: "blur" }
        ],
        cur_password: [
          { required: true, validator: validatePass2, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      let { old_password, new_password } = this.ruleForm;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$api
            .jpost("user.modify_password", { new_password, old_password })
            .then(res => {
              if (res.status === 1) {
                this.$success(res.msg);
                this.resetForm("ruleForm");
              } else {
                this.$error(res.msg);
              }
            }).catch(() => {});
        } else {
          this.$error("请检查之后，确认无误再提交");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style lang="scss" scoped>
.listbox {
  margin-top: 40px;
}
</style>>